package com.gregbugaj.reflector.test;

import java.io.Closeable;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Before;
import org.junit.Test;

import com.gregbugaj.reflector.renderer.AbstractRenderer;
import com.gregbugaj.reflector.renderer.HtmlRenderer;
import com.gregbugaj.reflector.renderer.TreeRenderer;
import com.gregbugaj.reflector.test.model.Address;
import com.gregbugaj.reflector.test.model.Customer;
import com.gregbugaj.reflector.test.model.Info;
import com.gregbugaj.reflector.test.model.TestEnum;
import com.gregbugaj.reflector.visitors.AbstractVisitor;
import com.gregbugaj.reflector.visitors.JSFDumpContext;
import com.gregbugaj.reflector.visitors.XMLVisitor;

public class SimpleTester {
	Calendar cal=GregorianCalendar.getInstance();

	HashMap<String,Integer> infomap=new HashMap<String,Integer>();

	@Before
	public void setup(){
		infomap.put("key1", 1);
		infomap.put("key2", 2);
		infomap.put("key3", 3);
	}



	@Test
	public void simpleTest2(){
		/*
		System.out.println("Running test");
		Info infoModel=new Info();
		infoModel.infoA="Some string with text";
		infoModel.dob=cal.getTime();

		String simpleArray[] = new String[3];
		simpleArray[0]="Greg";
		simpleArray[1]="Leszek";
		simpleArray[2] = "Roman";

		infoModel.simpleArray=simpleArray;

		Map<String, Integer> infoMap=new  HashMap<String, Integer>();
		infoMap.put("Age", 27);
		infoMap.put("Year", 1981);

		infoModel.infomap=infoMap;

		Map m1=new HashMap();
		m1.put("A" , "Item A");
		m1.put("B" , m1); //Cyclic reference
		m1.put("C", "Item C");

		Object objArray[] = new Object[5];
		objArray[0]="Greg";
		objArray[1]=new Integer(10);

		String[][] multiple=new String[2][2];
		multiple[0][0]="Hell 1";
		multiple[0][1] = multiple[0][0];
		multiple[1][0]="Hell 3";
		multiple[1][1]="Hell 4";
		infoModel.multipleArray=multiple;

		Customer cust = new Customer();
		cust.setAge(4);
		cust.setName("Satans little layer");


		List<Address> list = new ArrayList<Address>();
		Address a1 = new Address();
		a1.setAddress1("A1 One");
		a1.setAddress2("A1 Two");

		list.add(a1);

		cust.setAddressList(list);



		Customer cust = new Customer();
		cust.setAge(4);
		cust.setName("Satans little layer");
		List<Address> list = new ArrayList<Address>();
		Address a1 = new Address();
		a1.setAddress1("A1 One");
		a1.setAddress2("A1 Two");

		Customer possible1 = new Customer();
		possible1.setName("Possible 1");


		Customer possible2 =new Customer();
		possible2.setName("Possible 2");

		Customer possible3 =new Customer();
		possible3.setName("Possible 3");
		possible3.setScore(3.3f);
		possible3.setAge(20);

		List<Customer> possibleCustomerList=new ArrayList<Customer>();
		possibleCustomerList.add (possible1);
		possibleCustomerList.add (possible2);
		possibleCustomerList.add (possible3);

		a1.setPossibleCustomers(possibleCustomerList);

		list.add(a1);

		Address a2 = new Address();
		a2.setAddress1("A2 One");
		a2.setAddress2("A2 Two");


		Address a2Temp = new Address();
		a2Temp.setAddress1("a2Temp One");
		a2Temp.setAddress2("a2Temp Two");
		a2.setTempAddress(a2Temp);

		list.add(a2);



		Info i2=new Info();
		i2.enm=TestEnum.ONE;


		HashMap<String,String> moreInfo=new HashMap<String,String>();
		moreInfo.put("key1moreInfo", "moreInfo");
		moreInfo.put("key2moreInfo", "moreInfo2");
		moreInfo.put("key3moreInfo", "moreInfo3");

		cust.setAddressList(list);
		 */

		
		AbstractVisitor visitor = new XMLVisitor();
		visitor.visit(infomap);


		//a2Temp
		//m1
		//list
		//cust
		//multiple
		//infoModel
		//objArray
	
		try {
			JSFDumpContext context = visitor.getContext();			
			context.print();

			AbstractRenderer renderer=new TreeRenderer();
			renderer.render(context);

			FileWriter writer = new FileWriter(new File("c:\\temp_render.htm"));

			String template="/templates/template.txt";
			InputStream stream = this.getClass().getResourceAsStream(template);

			int result=-1;
			StringBuffer sb=new StringBuffer();
			while((result=stream.read())!=-1){
				sb.append((char)result);
			}
			String tempCode=sb.toString();
			tempCode = tempCode.replaceAll("%output%", renderer.getBufferToString());	
			writer.write(tempCode);
			writer.close();

		} catch (Exception e) {
			e.printStackTrace();
		}	
			
	}
	
	//@Test
	public void simpleTest1(){
		Address a2 = new Address();
		a2.setAddress1("A2 One");
		a2.setAddress2("A2 Two");


		Address a2Temp = new Address();
		a2Temp.setAddress1("a2Temp One");
		a2Temp.setAddress2("a2Temp Two");
		a2.setTempAddress(a2Temp);
		
		
		String obj="HEllo non reflect";
		AbstractVisitor visitor = new XMLVisitor();
		visitor.visit(a2);
		InputStream stream = null; 
		FileWriter writer  =null;
		try {
			JSFDumpContext context = visitor.getContext();			
			context.print();

			AbstractRenderer renderer=new TreeRenderer();
			renderer.render(context);

			writer= new FileWriter(new File("c:\\temp_render.htm"));

			String template="/templates/template.txt";
			stream = this.getClass().getResourceAsStream(template);

			int result=-1;
			StringBuffer sb=new StringBuffer();
			while((result=stream.read())!=-1){
				sb.append((char)result);
			}
			String tempCode=sb.toString();
			tempCode = tempCode.replaceAll("%output%", renderer.getBufferToString());	
			writer.write(tempCode);
			
			close(writer);
			close(stream);

		} catch (Exception e) {
			e.printStackTrace();
		}
	
	}
	
	private void close(Closeable resource){
		try{
			resource.close();
		}catch(Exception ex){
		}
	}
}
